#include "dish_module.h"
#include <iostream>

namespace TakeAwayPlatform
{
    DishModule::DishModule(DatabaseHandler& dbHandler) : dbHandler(dbHandler) {}

    bool DishModule::addDish(int merchant_id, const std::string& name, double price, const std::string& description, const std::string& category, int stock)
    {
        std::string sql = "INSERT INTO dishes (merchant_id, name, price, description, category, stock, status) VALUES (" +
                          std::to_string(merchant_id) + ", '" + name + "', " + std::to_string(price) + ", '" + description + "', '" +
                          category + "', " + std::to_string(stock) + ", 1)";
        return dbHandler.update(sql);
    }

    bool DishModule::updateDish(int dish_id, const std::string& name, double price, const std::string& description, const std::string& category, int stock)
    {
        std::string sql = "UPDATE dishes SET name = '" + name + "', price = " + std::to_string(price) + ", description = '" + description + "', category = '" +
                          category + "', stock = " + std::to_string(stock) + " WHERE dish_id = " + std::to_string(dish_id);
        return dbHandler.update(sql);
    }

    bool DishModule::deleteDish(int dish_id)
    {
        std::string sql = "DELETE FROM dishes WHERE dish_id = " + std::to_string(dish_id);
        return dbHandler.update(sql);
    }

    Json::Value DishModule::getDishesByMerchant(int merchant_id)
    {
        std::string sql = "SELECT dish_id, name, price, description, category, stock, status FROM dishes WHERE merchant_id = " + std::to_string(merchant_id);
        return dbHandler.query(sql);
    }

    bool DishModule::getDishDetails(int dish_id, std::string& name, double& price, std::string& description, std::string& category, int& stock)
    {
        std::string sql = "SELECT name, price, description, category, stock FROM dishes WHERE dish_id = " + std::to_string(dish_id);
        Json::Value result = dbHandler.query(sql);

        if (result.size() > 0)
        {
            name = result[0]["name"].asString();
            price = result[0]["price"].asDouble();
            description = result[0]["description"].asString();
            category = result[0]["category"].asString();
            stock = result[0]["stock"].asInt();
            return true;
        }

        return false;
    }
}